/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.great.ad;

import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.Intercepts;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.core.InterceptorStack;
import br.com.caelum.vraptor.interceptor.Interceptor;
import br.com.caelum.vraptor.resource.ResourceMethod;
import br.ufc.great.ad.Model.User;

/**
 *
 * @author great
 */
@Intercepts
public class AutenticationInterceptor implements Interceptor {

    private final UserSession userSession;
    private final Result result;
    
    public AutenticationInterceptor(UserSession userSession, Result result){
        this.userSession = userSession;
        this.result = result;
    }
    
    public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance) throws InterceptionException {
        result.redirectTo(LoginController.class).index();
    }

    public boolean accepts(ResourceMethod method) {
        System.out.println("Metodo accepts");
        return !this.userSession.isLogged() && method.containsAnnotation(Restricted.class);
    }
    
}
